package com.chenju.invokechainservice.utils;

import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.IntStream;

public class FunnyListsUtils {

    public FunnyListsUtils() {}

    public static <T> List<T> asArrayList(T[] array){
        if(array == null){
            return new ArrayList<>();
        }
        ArrayList<T> list = new ArrayList<>(array.length);
        list.addAll(Arrays.asList(array));
        return list;
    }

    public static <T> List<T> asArrayList(T obj) {
        if (obj == null) {
            return new ArrayList<>();
        }
        List<T> list = new ArrayList<>();
        list.add(obj);
        return list;
    }

    public static Map<String, String> groupItems(List<String> items) {
        Map<String, String> groupedMap = new HashMap<>();
        if (items == null) {
            return groupedMap;
        }
        IntStream.range(0, items.size() - 1)
                .filter(i -> i % 2 == 0)
                .forEach(i -> {
                    String key = items.get(i).toString();
                    String value = items.get(i + 1).toString();
                    groupedMap.put(key, value);
                });

        return groupedMap;
    }
}
